﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Common;

//大智慧主题收集
namespace HotTopic.collector
{
    class DzhCollector : AbstractCollector
    {
        private static readonly String DzhPath = Config.DzhPath;

//        private static void Main(string[] args)
//        {
//            DzhCollector collector = new DzhCollector();
//            var reusl = collector.GetConceptMap();
//
//            Console.ReadKey();
//        }

        protected override Dictionary<string, List<string>> GetIndustryMap()
        {
            return new Dictionary<string, List<string>>();
        }

        protected override Dictionary<string, List<string>> GetConceptMap()
        {
            String dictionary = $@"{DzhPath}\USERDATA\block";
            List<String> filePathList = GetFilePathsBySection("[大智慧概念]");
            Dictionary<String, List<String>> result = new Dictionary<string, List<string>>();
            foreach (var filePath in filePathList)
            {
                String topicName = Regex.Replace(filePath, @"\d", ""); //去掉字符串中的数字
                String path = $@"{dictionary}\{filePath}.BLK";
                var file = File.Open(path, FileMode.Open, FileAccess.Read,
                    FileShare.ReadWrite);
                List<String> stocks = new List<string>();
                using (BinaryReader reader = new BinaryReader(file))
                {
                    reader.ReadBytes(4); //丢弃
                    while (reader.PeekChar() > -1)
                    {
                        reader.ReadBytes(2); //丢弃
                        byte[] stockCodeBytes = reader.ReadBytes(6);
                        string stockCode = System.Text.Encoding.Default.GetString(stockCodeBytes).Trim();
                        stockCode = stockCode + (stockCode.StartsWith("6") ? ".SH" : ".SZ");
                        reader.ReadBytes(4); //丢弃
                        stocks.Add(stockCode);
                    }
                }
                result[topicName] = stocks;
            }
            return result;
        }

        private List<String> GetFilePathsBySection(string sectionName)
        {
            List<String> fileNameList = new List<string>();
            String path = $@"{DzhPath}\USERDATA\block\BLOCK.DEF";
            var file = File.Open(path, FileMode.Open, FileAccess.Read,
                FileShare.ReadWrite);
            using (StreamReader reader = new StreamReader(file, Encoding.Default))
            {
                bool begin = false;
                String line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (begin)
                    {
                        //到了下一节section
                        if (line.Contains("["))
                        {
                            break;
                        }
                        fileNameList.Add(line);
                    }
                    else if (line.Contains(sectionName))
                    {
                        begin = true;
                    }
                }
            }
            return fileNameList;
        }


        protected override Dictionary<string, List<string>> GetAreaMap()
        {
            return new Dictionary<string, List<string>>();
        }
    }
}